package com.googlecode.gwtbb.client.only.widget.form.entry;

import com.googlecode.gwtbb.client.only.widget.basic.MultiPictureFileUpload;
import com.googlecode.gwtbb.client.only.widget.basic.PictureFileUpload;
import com.googlecode.gwtbb.client.shared.validation.FieldId;
import com.googlecode.gwtbb.client.shared.validation.Failure;
import com.googlecode.gwtbb.client.shared.util.StringUtils;
import com.googlecode.gwtbb.client.shared.util.PictureUtils;
import com.googlecode.gwtbb.client.shared.entity.picture.Picture;
import com.google.gwt.user.client.ui.Widget;

import java.util.List;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jan 21, 2009
 */
public class SingleMultiPictureFileUploadFormEntry extends SingleCellFormEntry {
	private MultiPictureFileUpload entryMultiPictureFileUpload;
	private List<Picture> defaultPictures;

	public SingleMultiPictureFileUploadFormEntry(FieldId fieldId, List<Picture> defaultPictures, int row, int col, MultiPictureFileUpload entryMultiPictureFileUpload) {
		super(fieldId, null, row, col);
		this.defaultPictures = defaultPictures;
		this.entryMultiPictureFileUpload = entryMultiPictureFileUpload;
		clearValue();
	}

	public List<Picture> getDefaultPictures() {
		return defaultPictures;
	}

	public void setDefaultPictures(List<Picture> defaultPictures) {
		this.defaultPictures = defaultPictures;
	}

	public MultiPictureFileUpload getEntryMultiPictureFileUpload() {
		return entryMultiPictureFileUpload;
	}

	@Override
	public void clearValue() {
		entryMultiPictureFileUpload.setPictures(defaultPictures);
	}

	@Override
	public String getDefaultValue() {
		StringBuffer value = new StringBuffer();
		if (defaultPictures != null) {
			boolean appendComma = false;
			for (Picture p : defaultPictures) {
				if (appendComma)
					value.append(",");
				else
					appendComma = true;
				value.append(PictureUtils.getServerFileName(p));
			}
		}
		return value.length() > 0 ? value.toString() : null;
	}

	public Widget getEntryWidget() {
		return entryMultiPictureFileUpload;
	}

	public String getValue() {
		StringBuffer value = new StringBuffer();
		for (PictureFileUpload pfu : entryMultiPictureFileUpload.getPictureFileUploads()) {
			if (pfu.getStatus() == PictureFileUpload.STATUS_UPLOADED) {
				if (value.length() > 0)
					value.append(",");
				value.append(pfu.getServerFileName());
			}
		}
		return value.length() > 0 ? value.toString() : null;
	}

	@Override
	public boolean isChanged() {
		return super.isChanged();
	}

	public void redisplayFailures() {
		clearFailures();
		for (PictureFileUpload pfu : entryMultiPictureFileUpload.getPictureFileUploads()) {
			String errorMsg = pfu.getErrorMsg();
			if (StringUtils.isNotEmpty(errorMsg))
				addFailure(new Failure(errorMsg));
		}
	}

	@Override
	public void setDefaultValue(String defaultValue) {
		if (StringUtils.isNotEmpty(defaultValue))
			throw new UnsupportedOperationException("use setDefaultPictures instead");
	}

	public void setValue(String value) {
		// fileUploads cannot have their value set for security reasons.
		// However, a value being set is taken as an attempt to specify an already uploaded file.
		entryMultiPictureFileUpload.reset();
	}
}
